<template>
	<!-- 线上支付  支付详情-->
	<view>
		<!-- 地址 -->
		<!-- <view>
			<view class="card-center">
				<navigator url="/pages/address/address?source=1" class="address-section">
					<view class="order-content">
						<text class="yticon "></text>
						<view class="cen">
							<view class="top">
								<text class="name">{{addressData.name}}</text>
								<text class="mobile">{{addressData.mobile}}</text>
							</view>
							<text class="address ">{{addressData.address}} {{addressData.area}}</text>
						</view>
						<text class="yticon icon-you"></text>
					</view>
				</navigator>
			</view>
		</view> -->
		<view class="card-center">
			<view class=" goods-section">
				<!-- 商品列表 -->
				<view class="g-item">
					<!-- <image src="../../static/assets/img/commodity-1.png"></image> -->
					<image v-if="goodsObj.url" :src="goodsObj.url" mode="aspectFill"></image>
					<image v-else src="@/static/assets/img/no-img-1.png" mode="aspectFill"></image>
					<view class="right">
						<text class="title clamp">{{goodsObj.productName|isNullOrEmpty}}</text>
						<text class="spec">下单时间：{{goodsObj.createTime|isNullOrEmpty}}</text>
						<view class="price-box">
							<view class="price-bottom">
								<text class="goods-num">共1件商品</text>
								<text class="goods-price">实付款<text>¥{{goodsObj.productPrice|isNullOrEmpty}}</text></text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- 金额明细 -->
		<view class="card-center">
			<view class="yt-list">
				<view class="yt-list-cell b-b">
					<text class="cell-tit clamp">订单金额：</text>
					<text class="cell-tip">￥{{goodsObj.productPrice|isNullOrEmpty}}</text>
				</view>
				<view class="yt-list-cell b-b">
					<text class="cell-tit clamp">配送方式：</text>
					<text class="cell-tip">快递配送</text>
				</view>
				<view class="yt-list-cell ">
					<text class="cell-tit clamp">支付方式：</text>
					<text class="cell-tip">
						<u-radio-group v-model="radioValue">
							<u-radio name="1" active-color="#D40017" shape="circle">
								<view class="notice-select">微信</view>
							</u-radio>
							<u-radio name="2" active-color="#D40017" shape="circle">
								<view class="notice-select">支付宝</view>
							</u-radio>
							<u-radio name="3" active-color="#D40017" shape="circle">
								<view class="notice-select">银联</view>
							</u-radio>
						</u-radio-group>
					</text>
				</view>
			</view>
		</view>
		<view class="card-center">
			<view class="card-yes">
				<text class="card-btn" @click="submit">支付</text>
			</view>
		</view>
		<!-- 支付成功弹窗 -->
		<u-popup v-model="snapShotShow" :mask="true" mode="center" border-radius="14">
			<view class="snap-one">
				<image class="snap-image" src="../../static/product/snap-1.png"></image>
				<view class="snap-text">
					支付成功
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>

	import {
		valiDataNull
	} from "../../util/validate";
	import wx from 'weixin-js-sdk';
	export default {
		data() {
			return {
				radioValue: '',
				snapShotShow: false,
				maskState: 0, //优惠券面板显示状态
				desc: '', //备注
				payType: 1, //1微信 2支付宝
				couponList: [{
						title: '新用户专享优惠券',
						price: 5,
					},
					{
						title: '庆五一发一波优惠券',
						price: 10,
					},
					{
						title: '优惠券优惠券优惠券优惠券',
						price: 15,
					}
				],
				addressData: {
					name: '许小星',
					mobile: '13853989563',
					addressName: '金九大道',
					address: '山东省济南市历城区',
					area: '149号',
					default: false,
				},
				goodsObj: {}, //商品信息
				goodsid: null,
				pay: 0,
				body: '商品',
				subject: '商品',
				openid:''
			}
		},
		onLoad(options) {
			//商品数据
			console.log('线上支付options：', options)
			let pay = options.pay; //金额
			this.getGoodsDetails(options.goodsid);
			//判断是否是微信内部打开
			if (this.isWeiXin()) {
				console.log(" 是来自微信内置浏览器");
				this.getCode();
			} else {
				console.log("不是来自微信内置浏览器");
			}
		},
		methods: {
			//判断是否是微信打开
			isWeiXin() {
				var ua = window.navigator.userAgent.toLowerCase();
				console.log(
					ua
				); //mozilla/5.0 (iphone; cpu iphone os 9_1 like mac os x) applewebkit/601.1.46 (khtml, like gecko)version/9.0 mobile/13b143 safari/601.1
				if (ua.match(/MicroMessenger/i) == 'micromessenger') {
					return true;
				} else {
					return false;
				}
			},
			// 强制关注公众号，获取openid
			getCode() {
				if (sessionStorage.getItem("openid") && sessionStorage.getItem("openid") != "undefined") {
					return false;
				}
				var code = this.getUrlParam('code') // 截取路径中的code，如果没有就去微信授权，如果已经获取到了就直接传code给后台获取openId
				var local = window.location.href;
				var APPID = that.global.appid;
				if (code == null || code === '') {
					window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + APPID +
						'&redirect_uri=' + encodeURIComponent(local) +
						'&response_type=code&scope=snsapi_base&state=#wechat_redirect';
				} else {
					this.getOpenId(code) //把code传给后台获取用户信息
				}
			},
			//把code传给后台,得到openid
			getOpenId(code) {

				var that = this;
				that.$http({
					method: 'POST',
					data: {
						code: code
					},
					url: "/wxPay/codeToOpenid?code=" + code
				}).then(function(response) { //接口返回数据
					if (response.status == -1) {

						// 提示没有关注公众号 没有关注公众号跳转到关注公众号页面
						console.log('您还未关注公众号喔');

						return;
					} else {
						// 本地存储这个openid，并刷新页面
						sessionStorage.setItem("openid", JSON.parse(response.data.msg).openid);

					}

				});


			},
			getUrlParam(name) {
				var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
				var r = window.location.search.substr(1).match(reg);
				if (r != null) return unescape(r[2]);
				return null;
			},
			//获取商品详细信息
			getGoodsDetails(goodsId) { //0邮寄,1寄售
				var that = this;
				that.$http({
					method: 'GET',
					url: this.global.target + "/mall/transactionRecord/" + goodsId,
				}).then(function(response) {
					let data = response.data;
					if (data.code === 200) {
						if (!valiDataNull(data.data)) {
							that.goodsObj = {};
							that.goodsObj = data.data;

						}
					} else {
						that.$api.msg(data.msg);
					}
				}, function(error) {
					that.$api.msg(data.msg);
				})
			},
			//显示优惠券面板
			toggleMask(type) {
				let timer = type === 'show' ? 10 : 300;
				let state = type === 'show' ? 1 : 0;
				this.maskState = 2;
				setTimeout(() => {
					this.maskState = state;
				}, timer)
			},
			numberChange(data) {
				this.number = data.number;
			},
			changePayType(type) {
				this.payType = type;
			},
			submit() {
				if (this.radioValue) { //=== '1'
					//this.snapShotShow = true;
					if (valiDataNull(this.goodsObj)) {
						this.$api.msg(`未获取商品信息，请刷新重试~`);
						return;
					}


					this.goodsid = this.goodsObj.orderCode;
					this.pay = this.goodsObj.productPrice * 100;
					this.confirm();


				} else {
					this.$api.msg(`请选择支付方式`);
				}
			},
			//确认支付
			confirm: async function() {

				var that = this;
				
				if (this.radioValue == 1) {
					//微信

					if (this.isWeiXin()) {
						var that = this;
						let openid=sessionStorage.getItem("openid");
						that.$http({
							method: 'POST',
							data: {
								body: this.body,
								subject: this.subject,
								outTradeNo: this.goodsid,
								totalAmount: this.pay,
								openid:openid
							},
							url: "/wxPay/webPay"
							//url: this.global.target + "/wx/user/userinfopubscount" //this指data
						}).then(function(data) { //接口返回数据
							var that = this;
							let msg = JSON.parse(data.data.msg);
							wx.config({
								debug: false, // 开启调试模式
								appId: msg.appId, // 公众号的唯一标识
								timestamp: msg.timeStamp, // 生成签名的时间戳
								nonceStr: msg.nonceStr, // 生成签名的随机串
								signature: msg.paySign, // 签名
								jsApiList: ['chooseWXPay'] // 填入需要使用的JS接口列表，这里是先声明我们要用到支付的JS接口
							});

							//config验证成功后会调用ready中的代码
							wx.ready(function() {
								//弹出支付窗口
								wx.chooseWXPay({
									timestamp: msg.timeStamp, // 支付签名时间戳，
									nonceStr: msg.nonceStr, // 支付签名随机串，不长于 32 位
									package: msg.package, // 统一支付接口返回的prepay_id参数值，
									signType: 'HMAC-SHA256', // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
									paySign: msg.paySign, // 支付签名
									success: function(res) {
										// 支付成功后的回调函数
										uni.navigateTo({
											url: '/pages/money/paySuccess'
										});
									}
								});

							});
						});

					} else {
						that.$http({
							method: 'POST',
							data: {
								body: this.body,
								subject: this.subject,
								outTradeNo: this.goodsid,
								totalAmount: this.pay,
							},
							url: "/wxPay/wapPay"
						}).then(function(data) {
							//接口返回数据
							if(data.code==500){
								alert("服务器异常");
								return;
							}
							console.log(JSON.stringify(data))
							window.location.href = data.data;
							uni.navigateTo({
								url: '/pages/goods/unclaimedlist'
							});

						});
					}


				} else if (this.radioValue == 2) { //支付宝
					if (this.isWeiXin()) {
						alert("请使用手机浏览器打开并支付");
						return;
					} 
					var that = this;
					that.$http({
						method: 'POST',
						data: {
							body: this.body,
							subject: this.subject,
							outTradeNo: this.goodsid,
							totalAmount: this.pay/100
						},
						url: "/aliPay/wapPay"
						//url: this.global.target + "/wx/user/userinfopubscount" //this指data
					}).then(function(data) { //接口返回数据
						/* 下面的data.content就是后台返回接收到的数据 */
						const div = document.createElement('div')
						/* 此处form就是后台返回接收到的数据 */
						div.innerHTML = data.data
						document.body.appendChild(div)
						document.forms[0].submit()

					}, function(error) {
						console.log('接口返回错误数据:', error)
					})

				} else if (this.radioValue == 3) { //银联
					console.log('银联支付：');
					var that = this;
					that.$http({
						method: 'POST',
						data: {
							body: this.body,
							subject: this.subject,
							outTradeNo: this.goodsid,
							totalAmount: this.pay/100
						},
						url: "/unionPay/toPay"
					}).then(function(data) { //接口返回数据

						const div = document.createElement('div')
						/* 此处form就是后台返回接收到的数据 */
						div.innerHTML = data.data
						document.body.appendChild(div)
						document.forms[0].submit()
					}, function(error) {
						console.log('接口返回错误数据:', error)
					})
				}
			},

			stopPrevent() {}
		}
	}
</script>

<style lang="scss">
	page {
		background: $page-color-base;
		padding-bottom: 100upx;
	}

	/* 抢拍相关 */
	.snap-one {
		width: 376rpx;
		height: 172rpx;
		background: #FFFFFF;
		border-radius: 16rpx;
		display: -webkit-box;
		display: -webkit-flex;
		display: flex;
		-webkit-box-pack: center;
		-webkit-justify-content: center;
		justify-content: center;
		-webkit-box-align: center;
		-webkit-align-items: center;
		align-items: center;
	}

	.snap-image {
		width: 84rpx;
		height: 84rpx;
	}

	.snap-text {
		font-size: 50rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 700;
		color: #B30C0C;
		line-height: 72rpx;
		letter-spacing: 3rpx;
		margin-left: 16rpx;
	}

	/* 支付按钮相关 */
	.card-yes {
		margin-top: 50px;
		border-radius: 8rpx;
	}

	.card-btn {
		width: 622rpx;
		height: 82rpx;
		background: #B30C0C;
		position: revert;
		display: -webkit-flex;
		justify-content: center;
		-webkit-box-align: center;
		-webkit-align-items: center;

		font-size: 32rpx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: #FFFFFF;
		line-height: 44rpx;
		letter-spacing: 1rpx;
		border-radius: 8rpx;
	}

	/* 卡片 */
	.card-center {
		display: flex;
		justify-content: center;
		padding-top: 16rpx;
		box-shadow: 0rpx 8rpx 34rpx 0rpx rgba(211, 7, 8, 0.03);
		border-radius: 28rpx;
	}

	.address-section {
		padding: 40rpx 0px 30rpx 30rpx;
		width: 704rpx;
		height: 200rpx;
		border-radius: 14rpx;
		background: #fff;
		position: relative;

		.order-content {
			display: flex;
			align-items: center;
		}

		.icon-shouhuodizhi {
			flex-shrink: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			width: 90upx;
			color: #888;
			font-size: 44upx;
		}

		.cen {
			display: flex;
			flex-direction: column;
			flex: 1;
			font-size: 28upx;
			color: $font-color-dark;
		}

		.name {
			font-size: 32rpx;
			font-family: PingFangSC-Medium, PingFang SC;
			font-weight: 600;
			color: #000000;
			line-height: 44rpx;
		}

		.mobile {
			margin-left: 40rpx;
			font-size: 32rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #000000;
			line-height: 44rpx;
		}

		.address {
			margin-top: 20rpx;
			font-size: 32rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #000000;
			line-height: 44rpx;
			overflow: auto;
			height: 40px;
		}

		.icon-you {
			font-size: 45rpx;
			color: #D8D8D8;
			margin-right: 30upx;
			margin-top: -40rpx;
		}

		.a-bg {
			position: absolute;
			left: 0;
			bottom: 0;
			display: block;
			width: 100%;
			height: 5upx;
		}
	}

	.goods-section {
		width: 704rpx;
		background: #fff;
		border-radius: 8rpx;

		.g-header {
			display: flex;
			align-items: center;
			height: 84upx;
			padding: 0 30upx;
			position: relative;
		}

		.logo {
			display: block;
			width: 50upx;
			height: 50upx;
			border-radius: 100px;
		}

		.name {
			font-size: 30upx;
			color: $font-color-base;
			margin-left: 24upx;
		}

		.g-item {
			height: 220rpx;
			display: flex;
			margin: 4rpx 16rpx;
			padding-top: 12rpx;

			image {
				flex-shrink: 0;
				display: block;
				width: 192rpx;
				height: 194rpx;
				border-radius: 8rpx;
			}

			.right {
				flex: 1;
				padding-left: 24upx;
				overflow: hidden;
			}

			.title {

				font-size: 28rpx;
				font-family: PingFangSC-Medium, PingFang SC;
				font-weight: 600;
				color: #000000;
				line-height: 40rpx;
			}

			.spec {
				font-size: 2rpx;
				font-family: PingFangSC-Regular, PingFang SC;
				font-weight: 400;
				color: #6C6C6C;
				line-height: 60rpx;
				padding-top: 6rpx;

			}

			.price-bottom {
				position: relative;
				bottom: -60rpx;
			}

			.price-box {
				text-align: right;

				.goods-num {
					font-size: 20rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #98999A;
					line-height: 28rpx;
				}

				.goods-price {
					font-size: 30rpx;
					font-family: PingFangSC-Regular, PingFang SC;
					font-weight: 400;
					color: #D72020;
					line-height: 40rpx;
					margin-left: 20rpx;
				}
			}

			.step-box {
				position: relative;
			}
		}
	}

	.yt-list {
		/* margin-top: 16upx; */
		width: 704rpx;
		height: 344rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 8rpx 34rpx 0rpx rgba(211, 7, 8, 0.03);
		border-radius: 14rpx;
	}

	.b-b:after,
	.b-t:after {
		border-bottom: 1px solid #D6D6D6;
		width: 660rpx;
	}

	.yt-list-cell {
		height: 102rpx;
		display: flex;
		align-items: center;
		padding: 10upx 30upx 10upx 40upx;
		line-height: 70upx;
		position: relative;
		margin-top: 14rpx;

		&.cell-hover {
			background: #fafafa;
		}

		&.b-b:after {
			left: 30upx;
		}

		.cell-icon {
			height: 32upx;
			width: 32upx;
			font-size: 22upx;
			color: #fff;
			text-align: center;
			line-height: 32upx;
			background: #f85e52;
			border-radius: 4upx;
			margin-right: 12upx;

			&.hb {
				background: #ffaa0e;
			}

			&.lpk {
				background: #3ab54a;
			}

		}

		.cell-more {
			align-self: center;
			font-size: 24upx;
			color: $font-color-light;
			margin-left: 8upx;
			margin-right: -10upx;
		}

		.cell-tit {

			font-size: 28rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #000000;
			line-height: 40rpx;

			flex: 1;
			margin-right: 10upx;
		}

		.cell-tip {
			font-size: 28rpx;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #969696;
			line-height: 40rpx;

			&.disabled {
				color: $font-color-light;
			}

			&.active {
				color: $base-color;
			}

			&.red {
				color: $base-color;
			}
		}

		&.desc-cell {
			.cell-tit {
				max-width: 90upx;
			}
		}

		.desc {
			flex: 1;
			font-size: $font-base;
			color: $font-color-dark;
		}
	}

	/* 支付列表 */
	.pay-list {
		padding-left: 40upx;
		margin-top: 16upx;
		background: #fff;

		.pay-item {
			display: flex;
			align-items: center;
			padding-right: 20upx;
			line-height: 1;
			height: 110upx;
			position: relative;
		}

		.icon-weixinzhifu {
			width: 80upx;
			font-size: 40upx;
			color: #6BCC03;
		}

		.icon-alipay {
			width: 80upx;
			font-size: 40upx;
			color: #06B4FD;
		}

		.icon-xuanzhong2 {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 60upx;
			height: 60upx;
			font-size: 40upx;
			color: $base-color;
		}

		.tit {
			font-size: 32upx;
			color: $font-color-dark;
			flex: 1;
		}
	}

	.footer {
		position: fixed;
		left: 0;
		bottom: 0;
		z-index: 995;
		display: flex;
		align-items: center;
		width: 100%;
		height: 90upx;
		justify-content: space-between;
		font-size: 30upx;
		background-color: #fff;
		z-index: 998;
		color: $font-color-base;
		box-shadow: 0 -1px 5px rgba(0, 0, 0, .1);

		.price-content {
			padding-left: 30upx;
		}

		.price-tip {
			color: $base-color;
			margin-left: 8upx;
		}

		.price {
			font-size: 36upx;
			color: $base-color;
		}

		.submit {
			display: flex;
			align-items: center;
			justify-content: center;
			width: 280upx;
			height: 100%;
			color: #fff;
			font-size: 32upx;
			background-color: $base-color;
		}
	}

	/* 优惠券面板 */
	.mask {
		display: flex;
		align-items: flex-end;
		position: fixed;
		left: 0;
		top: var(--window-top);
		bottom: 0;
		width: 100%;
		background: rgba(0, 0, 0, 0);
		z-index: 9995;
		transition: .3s;

		.mask-content {
			width: 100%;
			min-height: 30vh;
			max-height: 70vh;
			background: #f3f3f3;
			transform: translateY(100%);
			transition: .3s;
			overflow-y: scroll;
		}

		&.none {
			display: none;
		}

		&.show {
			background: rgba(0, 0, 0, .4);

			.mask-content {
				transform: translateY(0);
			}
		}
	}

	/* 优惠券列表 */
	.coupon-item {
		display: flex;
		flex-direction: column;
		margin: 20upx 24upx;
		background: #fff;

		.con {
			display: flex;
			align-items: center;
			position: relative;
			height: 120upx;
			padding: 0 30upx;

			&:after {
				position: absolute;
				left: 0;
				bottom: 0;
				content: '';
				width: 100%;
				height: 0;
				border-bottom: 1px dashed #f3f3f3;
				transform: scaleY(50%);
			}
		}

		.left {
			display: flex;
			flex-direction: column;
			justify-content: center;
			flex: 1;
			overflow: hidden;
			height: 100upx;
		}

		.title {
			font-size: 32upx;
			color: $font-color-dark;
			margin-bottom: 10upx;
		}

		.time {
			font-size: 24upx;
			color: $font-color-light;
		}

		.right {
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			font-size: 26upx;
			color: $font-color-base;
			height: 100upx;
		}

		.price {
			font-size: 44upx;
			color: $base-color;

			&:before {
				content: '￥';
				font-size: 34upx;
			}
		}

		.tips {
			font-size: 24upx;
			color: $font-color-light;
			line-height: 60upx;
			padding-left: 30upx;
		}

		.circle {
			position: absolute;
			left: -6upx;
			bottom: -10upx;
			z-index: 10;
			width: 20upx;
			height: 20upx;
			background: #f3f3f3;
			border-radius: 100px;

			&.r {
				left: auto;
				right: -6upx;
			}
		}
	}
</style>
